package com.aliyun.messagemgt.dto.vo.weixin;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Xiaohang Lv
 * @Date: 2019/5/15 16:43
 */
public class WeixinNewsSendModelVO extends WeixinSendModelVO {

    private News news;

    public WeixinNewsSendModelVO() {
        super.setMsgtype("news");
        news = new News();
    }

    public News getNews() {
        return news;
    }

    public WeixinNewsSendModelVO setNews(News news) {
        this.news = news;
        return this;
    }

    public WeixinNewsSendModelVO setTitleByIndex(String title) {
        if (CollectionUtils.isEmpty(news.getArticles())) {
            News.Article article = new News.Article();
            article.setTitle(title);
            news.getArticles().add(article);
            return this;
        }
        news.getArticles().get(0).setTitle(title);
        return this;
    }

    public WeixinNewsSendModelVO setDescriptionByIndex(String description) {
        if (CollectionUtils.isEmpty(news.getArticles())) {
            News.Article article = new News.Article();
            article.setDescription(description);
            news.getArticles().add(article);
            return this;
        }
        news.getArticles().get(0).setDescription(description);
        return this;
    }

    public WeixinNewsSendModelVO setUrlByIndex(String url) {
        if (CollectionUtils.isEmpty(news.getArticles())) {
            News.Article article = new News.Article();
            article.setUrl(url);
            news.getArticles().add(article);
            return this;
        }
        news.getArticles().get(0).setUrl(url);
        return this;
    }

    public WeixinNewsSendModelVO setPicUrlByIndex(String picUrl) {
        if (CollectionUtils.isEmpty(news.getArticles())) {
            News.Article article = new News.Article();
            article.setPicurl(picUrl);
            news.getArticles().add(article);
            return this;
        }
        news.getArticles().get(0).setPicurl(picUrl);
        return this;
    }


    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    static class News {
        private List<Article> articles;

        News() {
            articles = new ArrayList<>();
        }

        public News setTitleByIndex(int index, String title) {
            if (articles.size() == 0 || articles.size() < index) {
                Article article = new Article();
                article.setTitle(title);
                articles.add(article);
                return this;
            }
            articles.get(index).setTitle(title);
            return this;
        }

        public News setDescriptionByIndex(int index, String description) {
            if (articles.isEmpty() || articles.size() < index) {
                Article article = new Article();
                article.setDescription(description);
                articles.add(article);
                return this;
            }
            articles.get(index).setDescription(description);
            return this;
        }

        public News setUrlByIndex(int index, String url) {
            if (articles.isEmpty() || articles.size() < index) {
                Article article = new Article();
                article.setUrl(url);
                articles.add(article);
                return this;
            }
            articles.get(index).setUrl(url);
            return this;
        }

        public News setPicUrlByIndex(int index, String picUrl) {
            if (articles.isEmpty() || articles.size() < index) {
                Article article = new Article();
                article.setPicurl(picUrl);
                articles.add(article);
                return this;
            }
            articles.get(index).setPicurl(picUrl);
            return this;
        }

        public List<Article> getArticles() {
            return articles;
        }

        public News setArticles(List<Article> articles) {
            this.articles = articles;
            return this;
        }

        static class Article {
            private String title;

            private String description;

            private String url;

            private String picurl;

            public String getTitle() {
                return title;
            }

            public Article setTitle(String title) {
                this.title = title;
                return this;
            }

            public String getDescription() {
                return description;
            }

            public Article setDescription(String description) {
                this.description = description;
                return this;
            }

            public String getUrl() {
                return url;
            }

            public Article setUrl(String url) {
                this.url = url;
                return this;
            }

            public String getPicurl() {
                return picurl;
            }

            public Article setPicurl(String picurl) {
                this.picurl = picurl;
                return this;
            }

            @Override
            public String toString() {
                return ToStringBuilder.reflectionToString(this);
            }
        }

        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this);
        }
    }

}
